package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.vo.topic.ReadTopicVO;
import com.cskaoyan.service.admin.MarketTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author RXC
 * @date 2022/6/27 16:02
 */
@RestController
@RequestMapping("admin/topic")
public class AdminTopicController {

    @Autowired
    MarketTopicService marketTopicService;

    /**
     * 显示所有优惠券，兼顾查询
     * @param title
     * @param subtitle
     * @param basePageInfo 注意sort
     */
    @RequestMapping("list")
    public BaseRespVo list(String title, String subtitle, BasePageInfo basePageInfo) {
        CommonData<MarketTopic> commonData = marketTopicService.list(title, subtitle,null, basePageInfo);
        return BaseRespVo.ok(commonData);
    }

    /**
     * todo 阅读量的校验问题
     * 添加专题，注意参数校验以及专题商品
     * 返回参数多了id, addTime, updateTime三项，后两者需要手动添加
     * @return
     */
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody MarketTopic marketTopic) {
        // 首先进行价格的校验
        BaseRespVo priceBaseRespVo = getPriceBaseRespVo(marketTopic);
        if (priceBaseRespVo != null) return priceBaseRespVo;

        marketTopic = marketTopicService.create(marketTopic);
        return BaseRespVo.ok(marketTopic);
    }

    // 价格和阅读量的校验
    private BaseRespVo getPriceBaseRespVo(MarketTopic marketTopic) {

        BigDecimal price = marketTopic.getPrice();
        // 校验价格
        if (price == null) {
            return BaseRespVo.invalidParameter("价格不能为空");
        }
        // 判断价格是否大于0
        if (price.compareTo(new BigDecimal(0)) < 0) {
            return BaseRespVo.invalidParameter("价格不能为负数");
        }
        // 校验阅读量
        String readCount = marketTopic.getReadCount();
        // 非空时才需要校验
        if (readCount != null) {
            if (readCount.length() == 1) {
                try {
                    Double.parseDouble(readCount);
                } catch (Exception e) {
                    return BaseRespVo.invalidParameter("阅读量不合法");
                }
            }
            if (readCount.length() > 1) {
                String numStr = readCount.substring(0, readCount.length() - 1);
                try {
                    Double.parseDouble(numStr);
                } catch (Exception e) {
                    return BaseRespVo.invalidParameter("阅读量不合法");
                }
            }
        }
        return null;
    }

    /**
     * 进入编辑页面，显示专题详情
     * @param id
     */
    @RequestMapping("read")
    public BaseRespVo read(Integer id) {
        ReadTopicVO readTopicVO = marketTopicService.read(id);
        if (readTopicVO == null) {
            return BaseRespVo.invalidData("该专题已不存在");
        }
        return BaseRespVo.ok(readTopicVO);
    }

    /**
     * 编辑专题
     * updateTime需要手动更改
     * @param marketTopic
     * @return marketTopic
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketTopic marketTopic) {
        // 首先进行价格的校验
        BaseRespVo priceBaseRespVo = getPriceBaseRespVo(marketTopic);
        if (priceBaseRespVo != null) return priceBaseRespVo;

        marketTopic = marketTopicService.update(marketTopic);
        return BaseRespVo.ok(marketTopic);
    }

    /**
     * 删除单个专题
     * @param marketTopic
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketTopic marketTopic) {
        int delete = marketTopicService.delete(marketTopic);
        return BaseRespVo.ok();
    }

    /**
     * 批量删除专题
     * @param {ids: [266, 268, 271]}
     * 使用Map接受比较方便
     */
    @RequestMapping("batch-delete")
    public BaseRespVo batchDelete(@RequestBody Map<String, List<Integer>> map) {
        List<Integer> ids = map.get("ids");
        int delete = marketTopicService.batchDelete(ids);
        return BaseRespVo.ok();
    }
}
